package com.ltf.controller.adm;

import com.ltf.entity.XJson;
import com.ltf.entity.XUser;
import com.ltf.service.LargeFileService;
import com.ltf.service.XUserService;
import com.ltf.utils.PackJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/adm/tea")
public class TeaController {
    //打印输出换成这个
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    XUserService xUserService;
    @Autowired
    LargeFileService largeFileService;

    /*
        @GetMapping("/findByGrade/{grade}")
    public XJson findByGrade(@PathVariable("grade") Integer grade){//查询该年级学生
        List<XUser> byGrade = xUserService.findByGrade(grade);
        return  PackJson.packX(200,"",byGrade);
    }
     */
    //教师下拉框选择、班级下拉框选择、课程下拉框选择、学期下拉框选择、模糊搜索，上面那里的是学生管理页面里面其中的一个查询该年级（实现了）功能

    @GetMapping("/findByRole")
    public XJson findByRole() {
        List<XUser> users = this.xUserService.findByRole("tea");
        return PackJson.packX(200, "", users);
    }

    //根据id查询单个教师
    @GetMapping("/findById/{uid}")
    public XJson findByUid(@PathVariable("uid") Integer uid) {
        XUser tea = this.xUserService.findByUid(uid);
        if (tea != null) {
            return PackJson.packX(200, "", tea);
        }
        return PackJson.packX(401, "不存在", "");
    }

    //添加
    @PostMapping("/add")
    public XJson add(XUser xUser,
                     @RequestParam(value = "mypic", required = false)
                     MultipartFile myfile) throws IOException {
        xUser.setRole("tea");
        xUser.setPwd("111");
        //进行图片处理，不为空时
        if (myfile != null) {
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.replace("-", "");
            if (xUser.getPic() != null && xUser.getPic().length() > 0) {
                this.largeFileService.delete(xUser.getPic());
            }
            //添加图片id
            xUser.setPic(uuid);
            this.largeFileService.add(uuid, myfile.getOriginalFilename(), myfile.getBytes());
        }
        this.xUserService.add(xUser);
        List<XUser> teas = this.xUserService.findByRole("tea");
        logger.debug(teas.toString());
        return PackJson.packX(200, "添加成功", teas);
    }

    //修改
    @PostMapping("/update")
    public XJson update(XUser xUser,
                        @RequestParam(value = "mypic", required = false)
                        MultipartFile myfile) throws IOException {
        xUser.setRole("tea");
        if (myfile != null) {
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.replace("-", "");
            if (xUser.getPic() != null && xUser.getPic().length() > 0) {
                this.largeFileService.delete(xUser.getPic());
            }
            xUser.setPic(uuid);
            this.largeFileService.add(uuid, myfile.getOriginalFilename(), myfile.getBytes());
        }
        this.xUserService.update(xUser);
        List<XUser> teas = this.xUserService.findByRole("tea");
        return PackJson.packX(200, "修改成功", teas);
    }

    //删除(将状态修改为冻结)
    @GetMapping("/del/{uid}")
    public XJson del(@PathVariable("uid") Integer uid) {
        //先查出来,再修改状态
        XUser stu = this.xUserService.findByUid(uid);
        if (stu != null) {
            this.xUserService.del(uid);
            List<XUser> teas = this.xUserService.findByRole("tea");
            return PackJson.packX(200, "删除成功", teas);
        }
        return PackJson.packX(401, "不存在", "");
    }
}
